package com.imooc.sell.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * 网页授权-》获取code-》获取access_token、openid
 * github：weixin-java-tools
 *
 * @author heizi
 * @date 2018/11/6 11:37
 */
@RestController
@RequestMapping("/weixin")
public class WeixinController {

    private static Logger logger = LoggerFactory.getLogger(WeixinController.class);

    /**
     * 废弃不用（使用sdk）
     * @param code
     */
    @GetMapping("/oauth")
    public void oauth(@RequestParam("code") String code) {
        logger.info("进入oauth方法：code={}",code);
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET" +
                "&code="+code+"&grant_type=authorization_code";
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject(url, String.class);
        logger.info("response={}",response);
    }
}
